package com.ronglian.register_mini_program.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ronglian.register_mini_program.dao.RegisterActivityMapper;
import com.ronglian.register_mini_program.dao.RegisterOrderMapper;
import com.ronglian.register_mini_program.dao.RegisterUserMapper;
import com.ronglian.register_mini_program.model.*;
import com.ronglian.register_mini_program.model.web.*;
import com.ronglian.register_mini_program.service.ActivityService;
import com.ronglian.register_mini_program.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("ActivityService")
public class ActivityServiceImpl implements ActivityService {
    /**
     * 表单提交数据
     *
     * @param registerBody
     * @return
     */
    @Autowired
    RegisterActivityMapper activityMapper;
    @Autowired
    RegisterOrderMapper orderMapper;
    @Autowired
    RegisterUserMapper userMapper;


    @Override
    public PageResult<ActivityBody> selectJoinActivityList(String openid, Integer page_num, Integer page_size) {
        /**
         * 1.通过uid关联的所有订单
         */
        RegisterOrderExample orderExample = new RegisterOrderExample();
        RegisterOrderExample.Criteria orderCriteria = orderExample.createCriteria();
        orderCriteria.andOpen_idEqualTo(openid);
        orderExample.setOrderByClause("create_time desc");
        //一个返回的集合
        List<ActivityBody> bodyList = new ArrayList<>();
        List<RegisterOrder> registerOrderList = null;
        if (page_num != null || page_size != null) {
            PageHelper.startPage(page_num, page_size);
            List<RegisterOrder> registerOrders = orderMapper.selectByExample(orderExample);
            registerOrderList = registerOrders;
        } else {
            registerOrderList = orderMapper.selectByExample(orderExample);
        }


        if (registerOrderList != null && registerOrderList.size() > 0) {
            //i简单的返回列表信息
            for (RegisterOrder registerOrder : registerOrderList) {
                ActivityBody activityBody = new ActivityBody();
                //从订单信息表中得到活动信息，然后遍历循环返回活动详情信息

                activityBody.setActivity_id(registerOrder.getActivity_id());
                RegisterActivityWithBLOBs activity = activityMapper.selectByPrimaryKey(registerOrder.getActivity_id());
                if (activity != null) {


                    //先验证活动的有效性
                    long end_time = activity.getEnd_time().getTime();
                    long now_time = System.currentTimeMillis();
                    //获取活动详情的时候，我会先判断这个时候，报名截至时间是否还有效，无效则修改、is_alive、字段
                    if (end_time < now_time) {
                        activity.setIs_alive(0);
                        activityMapper.updateByPrimaryKeyWithBLOBs(activity);
                        activity = activityMapper.selectByPrimaryKey(activity.getActivity_id());
                    }


                    activityBody.setActivity_title(activity.getActivity_title());


                    //验证活动的数量
                    RegisterOrderExample registerOrderExample = new RegisterOrderExample();
                    RegisterOrderExample.Criteria oc = registerOrderExample.createCriteria();
                    oc.andActivity_idEqualTo(activity.getActivity_id());
                    long count = orderMapper.countByExample(registerOrderExample);
                    Integer co = new Long(count).intValue();

                    activityBody.setCount(co);


                    if (activity.getEnd_time() != null) {

                        activityBody.setEnd_time(DateUtil.dateToStrLong(activity.getEnd_time()));
                    }
                    activityBody.setActivity_icon_url(activity.getActivity_icon_url());
                    activityBody.setSponsor_id(activity.getSponsor_id());
                    activityBody.setActivity_postion(activity.getActivity_postion());
                    activityBody.setIs_alive(activity.getIs_alive());
                    RegisterUserExample example = new RegisterUserExample();
                    RegisterUserExample.Criteria criteria = example.createCriteria();
                    criteria.andOpen_idEqualTo(activity.getSponsor_id());
                    List<RegisterUser> users = userMapper.selectByExample(example);
                    if (users != null && users.size() > 0) {
                        activityBody.setSponsor_name(users.get(0).getName());
                    }

                }
                bodyList.add(activityBody);

            }
        }
        PageInfo pageInfo = new PageInfo(bodyList);
        PageResult pageResult = PageResult.getPageResult(pageInfo);
        return pageResult;
    }

    @Override
    public PageResult<ActivityBody> selectCreateActivityList(String sponsor_id, Integer page_num, Integer page_size) {


        //在创建的活动表里关联发起人id
        RegisterActivityExample activityExample = new RegisterActivityExample();
        RegisterActivityExample.Criteria activityCriteria = activityExample.createCriteria();
        activityCriteria.andSponsor_idEqualTo(sponsor_id);
        activityExample.setOrderByClause("activity_id  desc");
        List<RegisterActivityWithBLOBs> activityList = null;

        if (page_num != null || page_size != null) {
            PageHelper.startPage(page_num, page_size);
            List<RegisterActivityWithBLOBs> activitys = activityMapper.selectByExampleWithBLOBs(activityExample);
            activityList = activitys;
        } else {
            activityList = activityMapper.selectByExampleWithBLOBs(activityExample);
        }

        //返回集合创建
        List<ActivityBody> activityBodyList = new ArrayList<>();
        if (activityList != null && activityList.size() > 0) {
            for (RegisterActivityWithBLOBs activity : activityList) {

                //先验证活动的有效性
                long end_time = activity.getEnd_time().getTime();
                long now_time = System.currentTimeMillis();
                //获取活动详情的时候，我会先判断这个时候，报名截至时间是否还有效，无效则修改、is_alive、字段
                if (end_time < now_time) {
                    activity.setIs_alive(0);
                    activityMapper.updateByPrimaryKeyWithBLOBs(activity);
                    activity = activityMapper.selectByPrimaryKey(activity.getActivity_id());
                }

                ActivityBody activityBody = new ActivityBody();
                activityBody.setActivity_id(activity.getActivity_id());
                activityBody.setSponsor_id(activity.getSponsor_id());
                RegisterUserExample example = new RegisterUserExample();
                RegisterUserExample.Criteria criteria = example.createCriteria();
                criteria.andOpen_idEqualTo(activity.getSponsor_id());
                List<RegisterUser> users = userMapper.selectByExample(example);
                if (users != null && users.size() > 0) {
                    activityBody.setSponsor_name(users.get(0).getName());
                }
                activityBody.setActivity_icon_url(activity.getActivity_icon_url());
                if (activity.getEnd_time() != null) {
                    activityBody.setEnd_time(DateUtil.dateToStrLong(activity.getEnd_time()));
                }
                activityBody.setActivity_postion(activity.getActivity_postion());

                //验证活动的数量
                RegisterOrderExample orderExample = new RegisterOrderExample();
                RegisterOrderExample.Criteria oc = orderExample.createCriteria();
                oc.andActivity_idEqualTo(activity.getActivity_id());
                long count = orderMapper.countByExample(orderExample);
                Integer co = new Long(count).intValue();

                activityBody.setCount(co);


//                activityBody.setCount(activity.getCount());
                activityBody.setIs_alive(activity.getIs_alive());
                activityBody.setActivity_title(activity.getActivity_title());
                activityBodyList.add(activityBody);
            }
        }
        PageInfo pageInfo = new PageInfo(activityBodyList);
        PageResult pageResult = PageResult.getPageResult(pageInfo);
        return pageResult;
    }

    @Override
    public List<RegisterActivityWithBLOBs> selectAllActivity() {
        return activityMapper.selectByExampleWithBLOBs(new RegisterActivityExample());
    }

    @Override
    public int insert(RegisterActivityWithBLOBs activity) {
        System.out.println("我来创建活动了，打印is_alive======" + activity.getIs_alive().toString());
        return activityMapper.insert(activity);

    }

    @Override
    public int update(RegisterActivityWithBLOBs activity) {
        RegisterActivityExample registerActivityExample = new RegisterActivityExample();
        RegisterActivityExample.Criteria criteria = registerActivityExample.createCriteria();
        criteria.andActivity_idEqualTo(activity.getActivity_id());
        return activityMapper.updateByExampleWithBLOBs(activity, registerActivityExample);
    }

    @Override
    public int delete(Integer activity_id) {
        return activityMapper.deleteByPrimaryKey(activity_id);
    }

    @Override
    public ActivityDetailBody findByPrimaryKey(Integer activity_id) {
        RegisterActivityWithBLOBs activity = activityMapper.selectByPrimaryKey(activity_id);
        if (activity == null) {
            return null;
        }
        long end_time = activity.getEnd_time().getTime();
        long now_time = System.currentTimeMillis();
        //获取活动详情的时候，我会先判断这个时候，报名截至时间是否还有效，无效则修改、is_alive、字段

        if (end_time < now_time) {
            activity.setIs_alive(0);
            activityMapper.updateByPrimaryKeySelective(activity);
            activity = activityMapper.selectByPrimaryKey(activity_id);
        }

        ActivityDetailBody activityBody = new ActivityDetailBody();
        activityBody.setActivity_id(activity.getActivity_id());
        activityBody.setSponsor_id(activity.getSponsor_id());
        RegisterUserExample example = new RegisterUserExample();
        RegisterUserExample.Criteria criteria = example.createCriteria();
        criteria.andOpen_idEqualTo(activity.getSponsor_id());
        List<RegisterUser> users = userMapper.selectByExample(example);
        if (users != null && users.size() > 0) {
            activityBody.setSponsor_name(users.get(0).getName());
        }
        activityBody.setActivity_image_url(activity.getActivity_image_url());
        activityBody.setActivity_background_url(activity.getActivity_background_url());
        activityBody.setActivity_icon_url(activity.getActivity_icon_url());
        activityBody.setEnd_time(DateUtil.dateToStrLong(activity.getEnd_time()));
        activityBody.setRequired_info(activity.getRequired_info());
        activityBody.setActivity_mark(activity.getActivity_mark());
        activityBody.setActivity_postion(activity.getActivity_postion());
        activityBody.setIs_alive(activity.getIs_alive());
        activityBody.setStart_time(DateUtil.dateToStrLong(activity.getStart_time()));
        RegisterOrderExample orderExample = new RegisterOrderExample();
        RegisterOrderExample.Criteria oc = orderExample.createCriteria();
        oc.andActivity_idEqualTo(activity_id);
        long count = orderMapper.countByExample(orderExample);
        Integer co = new Long(count).intValue();

        activityBody.setCount(co);
        activityBody.setRequired_info(activity.getRequired_info());
        activityBody.setActivity_title(activity.getActivity_title());
        return activityBody;
    }


}
